package icom.acm.manager.mircblog;

import icom.acm.bean.mircblog.VIEW_USER_MBMESSAGE;
import icom.acm.util.DBConnect;

import java.util.ArrayList;
import java.util.List;

public class View_User_MBMessageMgr {
	/**
	 * 通过微博编号得到视图
	 * @param db
	 * @return
	 */
	public VIEW_USER_MBMESSAGE selectByMid(Integer mid, DBConnect db){
		List<VIEW_USER_MBMESSAGE> list = new ArrayList<VIEW_USER_MBMESSAGE>();
		String sql = "SELECT * FROM VIEW_USER_MBMESSAGE WHERE VUMM_M_ID="+mid;
		
		db.prepareStatement(sql);
		db.excuteQuery();
		
		db.next();
		VIEW_USER_MBMESSAGE vumm = new VIEW_USER_MBMESSAGE(db);
		
		return vumm;
	}
	
	@SuppressWarnings("finally")
	public List<VIEW_USER_MBMESSAGE> selectsOBDate(DBConnect db,Integer uid, int start, int size) throws Exception{
		List<VIEW_USER_MBMESSAGE> list = null;
		try {
			String sql = "SELECT count(*) FROM " +
					"VIEW_USER_MBMESSAGE AS vumm, T_USERRELATION AS tur " +
					"WHERE vumm.VUMM_U_ID=" + uid + " OR (vumm.VUMM_U_ID=tur.UR_UED_ID AND tur.UR_U_ID=" + uid +
					") GROUP BY vumm.VUMM_M_ID ORDER BY VUMM_M_DATE DESC LIMIT "+start + "," + size;
			db.prepareStatement(sql);
			db.excuteQuery();		
			db.next();		
			int cnt = db.getInt(1);
			list = new ArrayList<VIEW_USER_MBMESSAGE>(cnt);
			sql = "SELECT vumm.VUMM_M_ID AS VUMM_M_ID," +
					"vumm.VUMM_U_ID AS VUMM_U_ID," +
					"vumm.VUMM_M_CONTENT AS VUMM_M_CONTENT," +
					"vumm.VUMM_M_DATE AS VUMM_M_DATE," +
					"vumm.VUMM_MT_ID AS VUMM_MT_ID," +
					"vumm.VUMM_M_ANNEX AS VUMM_M_ANNEX," +
					"vumm.VUMM_U_EMAIL AS VUMM_U_EMAIL," +
					"vumm.VUMM_U_NICKNAME AS VUMM_U_NICKNAME FROM " +
					"VIEW_USER_MBMESSAGE AS vumm, T_USERRELATION AS tur " +
					"WHERE vumm.VUMM_U_ID=" + uid + " OR (vumm.VUMM_U_ID=tur.UR_UED_ID AND tur.UR_U_ID=" + uid +
					") GROUP BY vumm.VUMM_M_ID ORDER BY VUMM_M_DATE DESC LIMIT "+start + "," + size;
	System.out.println("=======\n"+sql+"\n============");		
			db.prepareStatement(sql);
			db.excuteQuery();
			
			while (db.next()){
				VIEW_USER_MBMESSAGE e = new VIEW_USER_MBMESSAGE(db); 			
				list.add(e);
			}
		} catch (Exception ex){
			if (list==null) list = new ArrayList<VIEW_USER_MBMESSAGE>();
		} finally {
			return list;
		}
	}
}
